<?php
session_start();

if ( $_SESSION['manager'] )
{
    require_once('../inc/config.db.php');

    if ( is_numeric($_GET['id']) )
    {
	$result = mysql_query
	(
	    'SELECT `treatment_subscriber`.*,'
		.'DATE_FORMAT(`date_time`,\'%d.%m.%Y, %H:%i\') AS `date_time`,'
		.'DATE_FORMAT(`task_time`,\'%d.%m.%Y, %H:%i\') AS `task_time`,'
		.'(SELECT `manager_name` FROM `manager` WHERE `manager`.`id`=`treatment_subscriber`.`creat_manager_id`) AS `create_manager_name`,'
		.'(SELECT `manager_name` FROM `manager` WHERE `manager`.`id`=`treatment_subscriber`.`responsible_manager_id`) AS `responsible_manager_name`'
		.' FROM `treatment_subscriber`'
		.' WHERE `this_is_task`=1'
		.' AND `id`='.$_GET['id']
	);

	if ( $result )
	{
	    $task = mysql_fetch_assoc($result);

	    if ( !empty($_GET['comment']) )
	    {
		mysql_query(sprintf('INSERT INTO `task_comment` (`treatment_subscriber_id`,`manager_id`,`comment`,`comment_date`) VALUES (%d,%d,\'%s\',NOW())',$task['id'],$_SESSION['manager']['id'],mysql_escape_string($_GET['comment'])));
	    }

	    switch($_GET['action'])
	    {
		case	'info':
		    $result = mysql_query(sprintf('SELECT `task_comment`.*,DATE_FORMAT(`comment_date`,\'%%d.%%m.%%Y, %%H:%%i\') AS `comment_date`,(SELECT `manager_name` FROM `manager` WHERE `manager`.`id`=`task_comment`.`manager_id`) AS `manager_name` FROM `task_comment` WHERE `treatment_subscriber_id`=%d ORDER BY `comment_date`',$task['id']));

		    while( $row = mysql_fetch_assoc($result) )
		    {
			$history[] = $row;
		    }

		    print json_encode(array('code'=>200,'task'=>$task,'history'=>$history));
		    exit();
		    break;

		case	'close':
		    mysql_query('START TRANSACTION');
		    mysql_query(sprintf('INSERT INTO `task_comment` (`treatment_subscriber_id`,`manager_id`,`comment`,`comment_date`) VALUES (%d,%d,\'%s\',NOW())',$task['id'],$_SESSION['manager']['id'],'Задача закрыта'));
		    mysql_query(sprintf('UPDATE `treatment_subscriber` SET `close`=1 WHERE `id`=%d',$task['id']));
		    mysql_query('COMMIT');
		    break;

		case	'transfer':
		    if ( is_numeric($_GET['manager']) )
		    {
			$manager = mysql_fetch_assoc(mysql_query('SELECT `manager_name` FROM `manager` WHERE `id`='.$_GET['manager']));

			mysql_query('START TRANSACTION');
			mysql_query(sprintf('INSERT INTO `task_comment` (`treatment_subscriber_id`,`manager_id`,`comment`,`comment_date`) VALUES (%d,%d,\'%s\',NOW())',$task['id'],$_SESSION['manager']['id'],sprintf('Назначен новый ответственный: %s',$manager['manager_name'])));
			mysql_query(sprintf('UPDATE `treatment_subscriber` SET `responsible_manager_id`=%d WHERE `id`=%d',$_GET['manager'],$task['id']));
			mysql_query('COMMIT');
		    }
		    break;

		case	'comment':
		    if ( !empty($_GET['date']) )
		    {
			mysql_query('START TRANSACTION');
			mysql_query(sprintf('INSERT INTO `task_comment` (`treatment_subscriber_id`,`manager_id`,`comment`,`comment_date`) VALUES (%d,%d,\'%s\',NOW())',$task['id'],$_SESSION['manager']['id'],sprintf('Установлено новое время для задачи: %s',$_GET['date'])));
			mysql_query(sprintf('UPDATE `treatment_subscriber` SET `task_time`=\'%s\' WHERE `id`=%d',$_GET['date'],$task['id']));
			mysql_query('COMMIT');
		    }
		    break;
	    }

	    if ( $err = mysql_error() )
	    {
		print json_encode(array('code'=>500,'data'=>$err));
	    }
	    else
	    {
		print json_encode(array('code'=>200,'data'=>$task));
	    }
	}
    }
}
?>
